set more off
global dir "directory"
/*******************************************************************************
Program: 	05_MJW_Cov.do
Purpose: 	First stage estimates for Miller, Johnson and Wherry ACA mort paper
Date: 	 	April 10, 2019
Last Updated: 	June 20, 2019
*******************************************************************************/
global final "$dir/local/finaldata"
global temp "$dir/local/tempdata"
global acs "acsdirectory"
global output "$dir/acamort/output"

*Read in ACS extract for 2008-2017 years from IPUMS
use "$acs/acs0817v2", clear
numlabel _all, add

gen white=race==1
gen black=race==2
gen other=race>=3
gen hispanic=hispan>0
gen female=sex==2
gen married=marst==1
gen anyhi=hcovany==2
gen private=hcovpri==2
gen public=hcovpub==2
gen medicaid=hinscaid==2
gen medicare=hinscare==2
gen uninsured=anyhi==0
gen ssi=1*(incsupp>0)
replace ssi=0 if incsupp==99999

gen newage=2014-year+age
label var newage "Age in 2014"

gen earlyexp=inlist(statefip,10, 11, 25, 36, 50)
drop if earlyexp

gen treated14a=inlist(statefip,4, 5, 6, 8, 9, 10, 11, 15, 17, 19, 21, 24, ///
25, 26, 27, 32, 34, 35, 36, 38, 39, 41, 44, 50, 53, 54)
gen treated14b=statefip==33
gen treated15a=inlist(statefip,18, 42)
gen treated15b=statefip==2
gen treated16a=statefip==30
gen treated16b=statefip==22
gen expyear=.
replace expyear=2014 if treated14a
replace expyear=2015 if treated14b | treated15a
replace expyear=2016 if treated15b | treated16a
replace expyear=2017 if treated16b
gen expand=1*(treated14a | treated14b | treated15a | treated15b | treated16a ///
	| treated16b)
gen postexp=1*(year>=expyear & expand)

forval x=1/6{
	gen expm_`x'=1*(year-expyear==-`x' & expand)
}
replace expm_6=1 if year-expyear<-6 & expand
forval x=0/3{
	gen exp`x'=1*(year-expyear==`x' & expand)
}

gen lesshs=1*(educd<63)
label var lesshs "Less than high school degree"
gen less138=1*(poverty<=138)
label var less138 "Less than or equal to 138% FPL"

save "$final/acs1ststage", replace

*main analysis, ages 55-64 in 2014 with less than high school degree or
*income <= 138% FPL, citizens, no SSI receipt
set more off
use "$final/acs1ststage", clear
keep if (lesshs | less138) & citizen!=3 & inrange(newage,55,64) & ~ssi
mean uninsured medicaid if expand & ~postexp [pw=perwt]
preserve
gen ob=1
collapse (sum) ob if year==2014 [pw=perwt], by(expand)
format ob %21.0f
list
restore
preserve
gen ob=1
keep if year<2014
collapse (sum) ob [pw=perwt], by(expyear)
format ob %21.0f
list
drop if expyear==.
egen total=sum(ob)
gen share=ob/total
list
restore
preserve
gen ob=1
collapse (sum) ob if year==2014 [pw=perwt], by(statefip)
rename ob population
export excel using "$output/popcounts", replace first(var)
restore

*add for CBPP report
preserve
gen ob=1
collapse (sum) ob if year==2017 [pw=perwt], by(expand)
format ob %21.0f
list
restore

preserve
keep medicaid uninsured exp* statefip year perwt
save "$final/mainmedicaid", replace
restore

foreach x of var medicaid uninsured {
	reg `x' postexp i.statefip i.year [pw=perwt], cluster(statefip)
	outreg2 using "$output/firststage", append excel bdec(3) ctitle("`x'","age 55-64 target")
	reg `x' expm_6 expm_5 expm_4 expm_3 expm_2 exp0 exp1 exp2 exp3 i.statefip ///
	i.year [pw=perwt], cluster(statefip)
	outreg2 using "$output/firststage", append excel bdec(3) ctitle("`x'","age 55-64 target")
}
*descriptive stats for MHEC
mean white black hispanic uninsured medicaid newage poverty lesshs if expand & year<2014 [pw=perwt]
mean white black hispanic uninsured medicaid newage poverty lesshs if ~expand & year<2014 [pw=perwt]

*additional analysis, ages 19-64 in 2014 with less than high school degree or
*income <= 138% FPL, citizens, no SSI receipt
use "$final/acs1ststage", clear
keep if (lesshs | less138) & citizen!=3 & inrange(newage,19,64) & ~ssi
mean medicaid uninsured if expand & ~postexp [pw=perwt]
preserve
gen ob=1
collapse (sum) ob if year==2014 & expand [pw=perwt]
format ob %21.0f
list
restore
foreach x of var medicaid uninsured {
	reg `x' postexp i.statefip i.year [pw=perwt], cluster(statefip)
	outreg2 using "$output/firststage", append excel bdec(3) ctitle("`x'","age 19-64 target")
	reg `x' expm_6 expm_5 expm_4 expm_3 expm_2 exp0 exp1 exp2 exp3 i.statefip ///
	i.year [pw=perwt], cluster(statefip)
	outreg2 using "$output/firststage", append excel bdec(3) ctitle("`x'","age 19-64 target")
}

set more off
log using "$dir/acamort/code/GrossNotoWang.log", replace
use "$final/acs1ststage", clear
keep if (lesshs | less138) & citizen!=3 & inrange(newage,19,64) & ~ssi
saveold "$final/forsarah", replace
***lw just trying to understand "parametric event study" from Gross, Noto, Wang, 5/6

*our main event study
reg medicaid expm_6 expm_5 expm_4 expm_3 expm_2 exp0 exp1 exp2 exp3 i.statefip ///
	i.year [pw=perwt], cluster(statefip)

*removal of pre-trend using pre-data to estimate only
gen relyear=year-2014
gen pretrend=expand*relyear
reg medicaid pretrend i.year i.statefip [pw=perwt] if relyear<0
predict medhat
gen yres=medicaid-medhat
reg yres exp0 exp1 exp2 exp3 i.statefip i.year [pw=perwt], cluster(statefip)

*vs. removal of pre-trend using Gross method
reg medicaid pretrend exp0 exp1 exp2 exp3 i.statefip i.year [pw=perwt], cluster(statefip)

log close



*placebo #1
use "$final/acs1ststage", clear
keep if newage>=65
mean medicaid uninsured if expand & ~postexp [pw=perwt]
preserve
gen ob=1
collapse (sum) ob if year==2014 & expand [pw=perwt]
restore
foreach x of var medicaid uninsured {
	reg `x' postexp i.statefip i.year [pw=perwt], cluster(statefip)
	outreg2 using "$output/firststage", append excel bdec(3) ctitle("`x'","elderly")
	reg `x' expm_6 expm_5 expm_4 expm_3 expm_2 exp0 exp1 exp2 exp3 i.statefip i.year ///
	[pw=perwt], cluster(statefip)
	outreg2 using "$output/firststage", append excel bdec(3) ctitle("`x'","elderly")
}

*placebo #2
use "$final/acs1ststage", clear
keep if poverty>=400 & inrange(newage,55,64)
mean medicaid uninsured if expand & ~postexp [pw=perwt]
preserve
gen ob=1
collapse (sum) ob if year==2014 & expand [pw=perwt]
restore
foreach x of var medicaid uninsured {
	reg `x' postexp i.statefip i.year [pw=perwt], cluster(statefip)
	outreg2 using "$output/firststage", append excel bdec(3) ctitle("`x'","ages 55-64, 400% FPL")
	reg `x' expm_6 expm_5 expm_4 expm_3 expm_2 exp0 exp1 exp2 exp3 i.statefip i.year ///
	[pw=perwt], cluster(statefip)
	outreg2 using "$output/firststage", append excel bdec(3) ctitle("`x'","ages 55-64, 400% FPL")
}

*placebo #3
use "$final/acs1ststage", clear
gen treated10a=inlist(statefip,4, 5, 6, 8, 9, 10, 11, 15, 17, 19, 21, 24, ///
25, 26, 27, 32, 34, 35, 36, 38, 39, 41, 44, 50, 53, 54)
gen treated10b=statefip==33
gen treated11a=inlist(statefip,18, 42)
gen treated11b=statefip==2
gen treated12a=statefip==30
gen treated12b=statefip==22
drop expyear expand postexp expm* exp0 exp1 exp2 exp3 newage
gen expyear=.
replace expyear=2010 if treated10a
replace expyear=2015 if treated10b | treated11a
replace expyear=2016 if treated11b | treated12a
replace expyear=2017 if treated12b
gen expand=1*(treated10a | treated10b | treated11a | treated11b | treated12a ///
	| treated12b)
gen postexp=1*(year>=expyear & expand)

forval x=1/2{
	gen expm_`x'=1*(year-expyear==-`x' & expand)
}
replace expm_2=1 if year-expyear<-2 & expand
forval x=0/3{
	gen exp`x'=1*(year-expyear==`x' & expand)
}
gen newage=2010-year+age
label var newage "Age in 2010"

keep if inrange(year,2008,2013)
keep if (lesshs | less138) & citizen!=3 & inrange(newage,55,64) & ~ssi

mean medicaid uninsured if expand & ~postexp [pw=perwt]
preserve
gen ob=1
collapse (sum) ob if year==2010 & expand [pw=perwt]
restore
foreach x of var medicaid uninsured {
	reg `x' postexp i.statefip i.year [pw=perwt], cluster(statefip)
	outreg2 using "$output/firststage", append excel bdec(3) ctitle("`x'","ages 55-64, pre-aca")
	reg `x' expm_2 exp0 exp1 exp2 exp3 i.statefip i.year ///
	[pw=perwt], cluster(statefip)
	outreg2 using "$output/firststage", append excel bdec(3) ctitle("`x'","ages 55-64, pre-aca")
}


*adjustments for BOTEC in the appendix, added 8/6/19
set more off
use "$final/acs1ststage", clear
keep if inrange(age,20,64)

foreach x of var medicaid uninsured {
	reg `x' postexp i.statefip i.year [pw=perwt], cluster(statefip)
}

keep if inrange(age,55,64)
foreach x of var medicaid uninsured {
	reg `x' postexp i.statefip i.year [pw=perwt], cluster(statefip)
}

log close
